

package com.railway.news.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.railway.common.base.BaseStruts2Action;
import com.railway.common.util.Json;
import com.railway.common.util.StaticMap;
import com.railway.news.model.Category;
import com.railway.news.model.CategoryUserLink;
import com.railway.news.service.ICategoryManager;
import com.railway.news.vo.query.CategoryQuery;

/**
 * @author jiangcheng
 * @version 1.0
 * @since 1.0
 */

public class CategoryAction extends BaseStruts2Action implements Preparable,
		ModelDriven {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	// forward paths
	protected static final String QUERY_JSP = "/admin/news/category/query.jsp";
	protected static final String LIST_JSP = "/admin/news/category/list.jsp";
	protected static final String CREATE_JSP = "/admin/news/category/create.jsp";
	protected static final String EDIT_JSP = "/admin/news/category/edit.jsp";
	protected static final String SHOW_JSP = "/admin/news/category/show.jsp";
	// redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/news/Category/list.do";

	private ICategoryManager categoryManager;

	private String returnUrl;
	private Category category;
	java.lang.String id = null;
	private String[] items;
	private String param;

	public void setParam(String param) {
		this.param = param;
	}
	
	/**
	 * 
	 * @return
	 */
    public String getPermitinfoByCatId(){
	 
        Map paramap = new HashMap();
        
        String catId = this.getRequest().getParameter("catId");

        paramap.put("catId",catId);

		List list = (List) categoryManager.getUsernamesByMap(paramap);
		Map map = new HashMap();
		
		
		
		
		if (list != null) {
			
			for(int i = 0 ; i < list.size() ; i ++){
				
				CategoryUserLink cL= (CategoryUserLink)list.get(i);
				cL.setUsername(StaticMap.getUserValue(cL.getUserid()));
				
			}
	
			map.put("total", list.size());
			map.put("rows", list);
			 
		}
		return writeAjaxResponse(Json.toJson(map));

    }
	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			category = new Category();
		} else {
			
			category = (Category) categoryManager.getById(id);
			Map map = new HashMap();
			map.put("catId", id);
			try{
				
			 List list = categoryManager.getUsernamesByMap(map);
			
			StringBuffer msb = new StringBuffer();
			StringBuffer vsb = new StringBuffer();
			StringBuffer csb = new StringBuffer();
			
			StringBuffer msb1 = new StringBuffer();
			StringBuffer vsb1 = new StringBuffer();
			StringBuffer csb1 = new StringBuffer();
			if(list != null){
				
				for(int i = 0 ; i < list.size();i++){
					
					CategoryUserLink c = (CategoryUserLink)list.get(i);
					if("C".equals(c.getState())){
						String unames = StaticMap.getUserValue(c.getUserid());
						csb.append(unames);
						csb.append(",");
						
						String udis =  c.getUserid();
						csb1.append(udis);
						csb1.append(",");
					}else if("M".equals(c.getState())){
						String unames = StaticMap.getUserValue(c.getUserid());
						msb.append(unames);
						msb.append(",");
						
						String udis =  c.getUserid();
						msb1.append(udis);
						msb1.append(",");
					}else if("V".equals(c.getState())){
						String unames = StaticMap.getUserValue(c.getUserid());
						vsb.append(unames);
						vsb.append(",");
						
						String udis =  c.getUserid();
						vsb1.append(udis);
						vsb1.append(",");
					}
					
					
				}
				
			}
			category.setCheckernames(csb.toString());
			category.setVisitornames(vsb.toString());
			category.setMaintainernames(msb.toString());
			
			category.setCheckerid(csb1.toString());
			category.setVisitorid(vsb1.toString());
			category.setMaintainerid(msb1.toString());
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setCategoryManager(ICategoryManager manager) {
		this.categoryManager = manager;
	}

	public Object getModel() {
		return category;
	}

	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		return LIST_JSP;
	}

	public String listAjax() {
		CategoryQuery query = newQuery(CategoryQuery.class,
				DEFAULT_SORT_COLUMNS);
		Page page = categoryManager.findPage(query);
		Map map = new HashMap();
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
		return writeAjaxResponse(Json.toJson(map));
	}

	/** 查看对象 */
	public String show() {
		return SHOW_JSP;
	}

	/** 进入新增页面 */
	public String create() {
		return CREATE_JSP;
	}

	/** 保存新增对象 */
	public String save() {
		String fileTypes = this.getRequest().getParameter("fileTypes");
		if("VIDEO".equals(fileTypes) || "PHOTO".equals(fileTypes)){
			category.setCaTypes(fileTypes);
			category.setId(fileTypes);
			category.setCheckerid(this.getRequest().getParameter("checkerid"));
			category.setMaintainerid(this.getRequest().getParameter("maintainerid"));
			
		}  
		
		categoryManager.save(category);
		if (returnUrl != null) {
			try {
				this.getResponse().sendRedirect(returnUrl);
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
			return returnUrl;
		} else if("VIDEO".equals(fileTypes)){
			return "!/files/Files/list.do?fileTypes=VIDEO";
		} else if("PHOTO".equals(fileTypes)){
			return "!/files/Files/list.do?fileTypes=PHOTO";
		}else{
			return LIST_ACTION;
		}
	}

	/** 进入更新页面 */
	public String edit() {
		return EDIT_JSP;
	}

	/** 保存更新对象 */
	public String update() {
		categoryManager.update(this.category);

		if (returnUrl != null) {
			try {
				this.getResponse().sendRedirect(returnUrl);
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
			return returnUrl;
		} else {
			return LIST_ACTION;
		}
	}

	public String showCategoryTree() {

		return "/admin/news/category/categorytree.jsp";
	}

	/**
	 * 获得经过授权的专题列表
	 * 
	 * @return
	 */
	public String getCategoryList() {
		String current_user_id = (String) this.getRequest().getSession()
				.getAttribute("userId");
		CategoryUserLink culinkquery = new CategoryUserLink();

		culinkquery.setUserid(current_user_id);
		culinkquery.setState("M");
		//		
		List authlist = categoryManager.getPermitCategoriesByUser(culinkquery);
		Map authmap = new HashMap();
		if (authlist != null) {
			for (int i = 0; i < authlist.size(); i++) {
				CategoryUserLink culink = (CategoryUserLink) authlist.get(i);
				authmap.put(culink.getCategoryid(), "");
			}
		}

		List rt = new ArrayList();

		List list = (List) categoryManager.findAll();

		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				Category ct = (Category) list.get(i);
				Map map = new HashMap();
				if (!authmap.containsKey(ct.getId())) {
					map.put("chkDisabled", "true");
				}
				map.put("id", ct.getId());
				map.put("pId", ct.getParentId());
				map.put("name", ct.getNames());
				map.put("open", "true");
				rt.add(map);

			}
		}
		return writeAjaxResponse(Json.toJson(rt));

	}

	/** 删除对象 */
	public String delete() {
		if (items != null)
			for (String ids : items) {
				String[] idArray = ids.split(",");
				for (String id : idArray)
					categoryManager.removeById(id);
			}
		return LIST_ACTION;
	}

	/** 删除对象 */
	public String remove() {
		
		categoryManager.removeById(id);
		
		if (returnUrl != null) {
			try {
				this.getResponse().sendRedirect(returnUrl);
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
			return returnUrl;
		} else {
			return LIST_ACTION;
		}
	}

	public String getReturnUrl() {
		return returnUrl;
	}

	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}

}
